package cn.cdtu.tibetplan.controller;
import java.util.ArrayList;
import java.util.List;

import cn.cdtu.tibetplan.pojo.*;
import cn.cdtu.tibetplan.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import cn.cdtu.tibetplan.service.RolemenuService;

/**
 * controller
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/rolemenu")
public class RolemenuController {

	@Autowired
	private RolemenuService rolemenuService;
	
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<Rolemenu> findAll(){
		return rolemenuService.findAll();
	}
	
	
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(int page, int rows){
		return rolemenuService.findPage(page, rows);
	}
	
	/**
	 * 增加
	 * @param rolemenu
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody Rolemenu rolemenu){
		try {
			rolemenuService.add(rolemenu);
			return new Result(true, "增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "增加失败");
		}
	}
	
	/**
	 * 修改
	 * @param rolemenu
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody Rolemenu rolemenu){
		try {
			rolemenuService.update(rolemenu);
			return new Result(true, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "修改失败");
		}
	}	
	
	/**
	 * 获取实体
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public Rolemenu findOne(Long id){
		return rolemenuService.findOne(id);		
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(Long [] ids){
		try {
			rolemenuService.delete(ids);
			return new Result(true, "删除成功"); 
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
		/**
	 * 查询+分页
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody Rolemenu rolemenu, int page, int rows  ){
		return rolemenuService.findPage(rolemenu, page, rows);		
	}

	@Autowired
	private MenuService menuService;
	//获取某角色菜单列表树
	@RequestMapping("/getMenuByRoleId")
	@ResponseBody
	public List<Menus> getUserMenu(int roleId){
		Rolemenu rm=new Rolemenu();
		rm.setRoleid(roleId);
		//PageResult rolePageResult = rolemenuService.findPage(rm, 1, 0);
		List<Rolemenu> roleRows = rolemenuService.findByRoleId(rm);//角色的菜单列表
		List<Integer> menuids=new ArrayList<>();//角色菜单id集合
		for(Rolemenu rlm:roleRows){
			if(rlm.getMenuid()!=0){
				menuids.add(rlm.getMenuid());
			}
		}
		System.out.println(menuids.size()+"333");
		List<Menus> allMenus = getAllMenus();
		for(Menus m:allMenus){
			if(menuids.contains(m.getId())){
				m.setChecked(true);
			}
			List<Menus> children = m.getChildren();
			if(children!=null&&children.size()>0){
				for(Menus ms:children){
					if(menuids.contains(ms.getId())){
						ms.setChecked(true);
					}
				}
			}
		}
		return allMenus;

	}

	//获取所有菜单
	private List<Menus> getAllMenus(){
		final List<Menu> menuRows = findMenuByPId(0);//一级菜单 pId=0
		List<Menus> result=new ArrayList<>();
		for(Menu mr:menuRows){
			List<Menu> childrenMenu = findMenuByPId(mr.getId());
			Menus ms=new Menus();
			ms.setId(mr.getId());
			ms.setHref(mr.getLink());
			ms.setTitle(mr.getName());
			ms.setSpread(true);
			if(childrenMenu.size()>0){
				List<Menus> children=new ArrayList<>();
				Menus mc=new Menus();
				for(Menu m2:childrenMenu){
					mc.setId(m2.getId());
					mc.setTitle(m2.getName());
					mc.setHref(m2.getLink());
					mc.setSpread(true);
					children.add(mc);
				}
				ms.setChildren(children);
			}
			result.add(ms);
		}
		return  result;
	}

	private List<Menu> findMenuByPId(int pId){
		Menu m=new Menu();
		m.setPid(pId);
		return menuService.findByPId(m);
	}
	
}
